package ch.zbw.rre;

public class SimpleListe implements Liste{

	private Element[] elemente;
	private int numbreOfElements;
	private final int MAXEL = 20;
	
	public SimpleListe() {
		elemente =new Element[MAXEL];
		numbreOfElements = 0;
	}
	
	public int numbreOfElements(){
		return numbreOfElements;
	}
	

	@Override
	public boolean insertFirst(Element x) {
		if (numbreOfElements < MAXEL){
			if(numbreOfElements == 0){
				elemente[0] = x;
				numbreOfElements++;
				return true;
			}
			if(numbreOfElements>0){
			
				for(int y = numbreOfElements; y > 0; y--){
					elemente[y] = elemente[y-1];
				}
				elemente[0] = x;
				numbreOfElements++;
				return true;
			}
		}		
		return false;
	}

	@Override
	public boolean insertLast(Element x) {
		if (numbreOfElements < MAXEL){
			elemente[numbreOfElements] = x;
			numbreOfElements ++;
			return true;
		}
		
		return false;
	}

	@Override
	public boolean empty() {
		if(numbreOfElements == 0){
			return true;
		}
		return false;
	}

	@Override
	public Element getFirst() {
		if (numbreOfElements < MAXEL){
			return elemente[0];
				}
		return null;
	}

	@Override
	public Element getLast() {
		if (numbreOfElements < MAXEL){
			return elemente[numbreOfElements-1];
		}
		return null;
	}

	@Override
	public Element search(int id) {
		for (int x = 0; x < numbreOfElements; x++){
			Element temp;
			temp = elemente[x];
			if(temp.getId() == id){
				return elemente[x];
			}	
		}
		return null;
	}

	@Override
	public boolean delete(int id) {
		for (int x = 0; x < numbreOfElements; x++){
			Element temp;
			temp = elemente[x];
			if(temp.getId() == id){
				elemente[x] = null;
				for(int y = x; y+1 < numbreOfElements; y++){
					elemente[y] = elemente[y+1];
				}
				numbreOfElements --;
				elemente[numbreOfElements] = null;
				return true;
			}
		}
		return false;
	}

	@Override
	public void show() {
		for (int i = 0; i < MAXEL; i++){
			System.out.println("Name: " + elemente[i].getName());
			System.out.println("ID: " + elemente[i].getId());
		}
		
	}

/*	@Override
	public boolean change(int id1, int id2) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArListe sublist(int pos1, int pos2) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArListe searchElements(int key1, int key2) {
		// TODO Auto-generated method stub
		return null;
	}
*/
}
